<?php
$show_company_info = 'style="display:none"';
$user = get_current_user_id();
$user_isadmin = user_can($user, 'edit_dashboard');
$user_meta = get_user_meta($result->user_id, 'company_info');
$user_meta = $user_meta[0];
$user_data = get_user_by('id', $result->user_id);

if($user_data && is_array($user_data->wp_capabilities)) {
    foreach ($user_data->wp_capabilities as $key => $value) {
        $role = $key;
    }
}
?>
<script type="text/javascript" >
    stSocialMediaToolbox.load_tooltip();
    stSocialMediaToolbox.on_submitform('#userForm', function(data){
        stSocialMediaToolbox.loadPage('userList', '', data);
    });
    jQuery(document).ready(function() {
    <?php
    if($user_isadmin && $role == 'editor'){
        echo "jQuery('#company_info').html(jQuery('#company_meta').html());";
    }
    ?>
    <?php
    if($role == 'author'){
        echo "jQuery('#user_list').css('display', 'block');";
        echo "jQuery('#parent_id').addClass('required');";
    }
    ?>
    });
    function selectUser(){
        if(jQuery('#role').val() == 'author'){
            jQuery('#user_list').css('display', 'block');
            jQuery('#parent_id').addClass('required');
            jQuery('#company_info').html('');
        }else{
            jQuery('#user_list').css('display', 'none');
            jQuery('#parent_id').removeClass('required');
            jQuery('#company_info').html(jQuery('#company_meta').html());
        }

    }
</script>
<div class="w100p bgl">
    <div class="w100p bgr p5b">
        <form class="stform" id="userForm" action="<?php echo admin_url('/admin.php?page=' . $config['plugin_name'] . '&action=' . $current_action) ?>" method="post" >
            <ul>
                <?php                
                if($user_isadmin){
                    $show_company_info = 'style="display:block"';
                ?>

                <li>
                    <label><?php _e('User role', $config['plugin_name'])?>:</label>
                    <div>
                        <select onchange="selectUser()" class="required" id="role" name="data[role]" tabindex="1">
                            <option value="">Select user's role</option>
                            <option <?php echo $role == 'editor'? 'selected':''?> value="editor">Client's administrator</option>
                            <option <?php echo $role == 'author'? 'selected':''?> value="author">Client's user</option>
                        </select>
                    </div>
                    <br clear="both" />
                </li>
                <li id="user_list" style="display: none">
                    <label><?php _e('Select parent user', $config['plugin_name'])?>:</label>
                    <div class="required">
                    <select class="" id="parent_id" name="parent_id" tabindex="1">
                        <option value=""><?php _e('Select admin user', $config['plugin_name'])?></option>
                        <?php
                            foreach ($users as $user) {
                                $user_data = get_user_by('id', $user->ID);
                                if ($user_data){
                                    foreach ($user_data->wp_capabilities as $key => $value) {
                                        $role = $key;
                                    }
                                }
                                if($role == 'editor'){
                                    if($user->ID == $result->parent_id)
                                        $select = 'selected';
                                    else
                                        $select = '';
                                    ?>
                                    <option <?php echo $select?> value="<?php echo $user->ID?>">
                                            <?php echo $user->user_login?>
                                    </option>
                                    <?php }?>
                                <?php }?>
                    </select>
                    </div>
                </li>
                <?php } else{?>
                <input type="hidden" value="author" name="data[role]" />
                <input type="hidden" value="<?php echo get_current_user_id()?>" name="parent_id" />
                
                <?php }?>
                
                <li>
                    <label><?php _e('User name', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="<?php echo $result->user_login ?>" class="required" name="data[user_login]" id="user_login" type="text"/>
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('E-mail', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="<?php echo $result->user_email ?>" class="required email" name="data[user_email]" id="user_email" type="text"/>
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('Re-E-mail', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="<?php echo $result->user_email ?>" equalto="#user_email" id="user_email1" type="text"/>
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('Password', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="" <?php echo empty($result->user_id)? 'class="required"':''?>  name="data[user_pass]" id="user_pass" type="password"/>
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('Re-password', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="" equalto="#user_pass" id="password2" type="password"/>
                    </div>
                    <br clear="both" />
                </li>
                <div id="company_info" <?php echo $show_company_info?>>
                
                </div>

                <li>
                    <label></label>
                    <div>
                        <input value="1" class="" name="send_email" id="send_email" type="checkbox"/>
                        <label style="width:150px" class=""><?php _e('Send this password to the new user by email.', $config['plugin_name']) ?></label>
                    </div>
                    <br clear="both" />
                </li>
                
                <li>
                    <input type="submit" value="Submit"/>
                </li>
            </ul>
            <input type="hidden" value="<?php echo $result->user_id ?>" id="ID" name="data[ID]"/>
            <input type="hidden" value="<?php echo $result->id ?>" id="social_user_id" name="social_user_id"/>
        </form>
    </div>
</div>

<div id="company_meta" style="display:none" >
    <li>
        <label><?php _e('Company name', $config['plugin_name']); ?>:</label>
        <div class="required">
            <input value="<?php echo $user_meta['company_name'] ?>" class="required" name="meta[company_name]" id="total_value" type="text"/>
        </div>
        <br clear="both" />
    </li>
    <li>
        <label><?php _e('Contact person name', $config['plugin_name']); ?>:</label>
        <div class="required">
            <input value="<?php echo $user_meta['contact_name'] ?>" class="required" name="meta[contact_name]" id="product_id" type="text"/>
        </div>
        <br clear="both" />
    </li>
    <li>
        <label><?php _e('Contact person phone number', $config['plugin_name']); ?>:</label>
        <div class="required">
            <input value="<?php echo $user_meta['contact_phone'] ?>" class="required" name="meta[contact_phone]" id="total_value" type="text"/>
        </div>
        <br clear="both" />
    </li>
    <li >
        <label><?php _e('Address', $config['plugin_name']); ?>:</label>
        <div class="required">
            <input value="<?php echo $user_meta['address'] ?>" class="required" name="meta[address]" id="address" type="text"/>
        </div>
        <br clear="both" />
    </li>
    <li >
        <label><?php _e('Zip code', $config['plugin_name']); ?>:</label>
        <div class="required">
            <input value="<?php echo $user_meta['zip_code'] ?>" class="required" name="meta[zip_code]" id="zip_code" type="text"/>
        </div>
        <br clear="both" />
    </li >
    <li>
        <label><?php _e('City', $config['plugin_name']); ?>:</label>
        <div class="required">
            <input value="<?php echo $user_meta['city'] ?>" class="required" name="meta[city]" id="city" type="text"/>
        </div>
        <br clear="both" />
    </li>
    <li >
        <label><?php _e('Country', $config['plugin_name']); ?>:</label>
        <div class="required">
            <input value="<?php echo $user_meta['country'] ?>" class="required" name="meta[country]" id="country" type="text"/>
        </div>
        <br clear="both" />
    </li>
</div>